package com.yunshu.yunshuapp.tongxunlu;

import android.os.Handler;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yunshu.yunshuapp.NavigationBar;
import com.yunshu.yunshuapp.R;
import com.yunshu.yunshuapp.kuaixun.TypeListAdapter;
import com.yunshu.yunshuapp.network.YSCallback;
import com.yunshu.yunshuapp.network.YSRequest;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

public class TongxunluActivity extends ButtonsListActivity {

    protected ContactListAdapter adapter;
    protected YSRequest request1;
    private String cities;
    private String channelName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        request1 = new YSRequest(this);
        cities = "";
        channelName = "";

        super.onCreate(savedInstanceState);
        navigationBar.setTitle("企业通讯录");

    }

    protected void initRecyleView() {
        super.initRecyleView();

        adapter = new ContactListAdapter(getBaseContext());
        adapter.activity = this;
        mRecyclerView.setAdapter(adapter);
    }


    protected void refresh() {
        if (!request1.getIsCompleted()) return;
        pageIndex = 1;
        request1.GET(YSRequest.TONGXUNLU_LIST, "page=1&channelname="+channelName + "&ext="+cities, new YSCallback() {
            @Override
            public void onResponse(int isSuccess, JsonNode result) {
                endRefreshing();
                if (isSuccess != YSRequest.REQUEST_OK) return;
                if (result == null || !result.isArray()) return;
                Iterator<JsonNode> iterator = result.iterator();
                ArrayList<Contact> temp = new ArrayList<Contact>();
                ObjectMapper mapper = new ObjectMapper();
                while (iterator.hasNext()){
                    String jsonstr = iterator.next().toString();
                    try {
                        Contact model = mapper.readValue(jsonstr, Contact.class);
                        temp.add(model);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                adapter.setModels(temp);
                pageIndex +=1;
                reloadData();
            }
        });
    }

    protected void loadmore() {
        if (!request1.getIsCompleted()) return;
        request1.GET(YSRequest.TONGXUNLU_LIST, "page="+pageIndex, new YSCallback() {
            @Override
            public void onResponse(int isSuccess, JsonNode result) {
                endRefreshing();
                if (isSuccess != YSRequest.REQUEST_OK) return;
                if (!result.isArray()) return;
                Iterator<JsonNode> iterator = result.iterator();
                ArrayList<Contact> temp = new ArrayList<Contact>();
                ObjectMapper mapper = new ObjectMapper();
                while (iterator.hasNext()){
                    String jsonstr = iterator.next().toString();
                    try {
                        Contact model = mapper.readValue(jsonstr, Contact.class);
                        temp.add(model);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                pageIndex +=1;
                adapter.addFooterItem(temp);
                reloadData();
            }
        });
    }

    private void reloadData() {
        runOnUiThread(new Runnable() {      // UI thread
            @Override
            public void run() {
                adapter.notifyDataSetChanged();
            }
        });
    }
    private void endRefreshing() {
        runOnUiThread(new Runnable() {      // UI thread
            @Override
            public void run() {
                swipeRefreshLayout.setRefreshing(false);
            }
        });

    }

    @Override
    protected void onFilterChange(String types, String cities) {
        this.channelName = types;
        if (types.equalsIgnoreCase("全部"))
            this.channelName ="";
        this.cities = cities;

        if (cities.equalsIgnoreCase("全部"))
            this.cities ="";
        this.refresh();
    }

}
